﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Windows.Input;

using System.Windows;

using GWApplication;

using FXnaAppBaseWpf;

using Microsoft.Win32;

using DotSpatial.Data;

using FXnaAppBase;

using System.IO;

using FXnaInfrastructure;




namespace GWPresentation
{
    public static class AppBaseCommandsBinder
    {

           /// <summary>
        /// 将命令绑定到窗体
        /// </summary>
        /// <param name="window"></param>
        public static void BindCommandsToWindow(Window window)
        {

            CommandBinding cbAddImage = new CommandBinding();
            cbAddImage.Command = AppBaseCommands.AddImage;
            cbAddImage.CanExecute += new CanExecuteRoutedEventHandler(cbAddImage_CanExecute);

            cbAddImage.Executed += new ExecutedRoutedEventHandler(cbAddImage_Executed);

            window.CommandBindings.Add(cbAddImage);





            CommandBinding cbRemoveImage = new CommandBinding();

            cbRemoveImage.Command = AppBaseCommands.RemoveImage;
            cbRemoveImage.CanExecute += new CanExecuteRoutedEventHandler(cbRemoveImage_CanExecute);
            cbRemoveImage.Executed += new ExecutedRoutedEventHandler(cbRemoveImage_Executed);

            window.CommandBindings.Add(cbRemoveImage);



            //AppBaseCommands.AddImage


        }

        static void cbRemoveImage_Executed(object sender, ExecutedRoutedEventArgs e)
        {

            Bitmap bm = e.Parameter as Bitmap;

            BitmapCollection collection = ProjectService.CurrentService.Project.CommonData.Images;

            if (collection.IsExist(bm.FileName))
            {



                collection.RemoveImage(bm);

                //资源文件夹里的图像是否要删除呢？，直接删除的话，该资源是否被占用呢？
                //默认先不删除吧

                
            }

            


            
        }

        static void cbRemoveImage_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = true;
            e.Handled = true;
            
           
        }

        static void cbAddImage_Executed(object sender, ExecutedRoutedEventArgs e)
        {

            //ProjectService.CurrentService.Project.CommonData.Images


            

            OpenFileDialog ofd = new OpenFileDialog();
            ofd.CheckFileExists = true;

            if (ofd.ShowDialog() == true)
            {
                string fpath = ofd.FileName;

                //需要的操作可能包括，
                //复制图片资源到项目
                //包装文件引用，并添加


               string ImagesPath=  ProjectService.CurrentService.GetProjPath(ProjPath.Images);


               BitmapCollection collection = ProjectService.CurrentService.Project.CommonData.Images;


               string filename = Path.GetFileName(fpath);


               if (collection.IsExist(filename))
               {
                   MessageBox.Show("相同的文件名已经存在");
                   return;
                   
               }


                ///如果移动文件成功

               if (FileHelper.MoveFile(fpath, ImagesPath + filename))
               {


                   Extent ext = ProjectService.CurrentService.Project.CommonData.WorkBoundary.LocalExtent;


                   Bitmap mb = new Bitmap(filename, ext);


                   collection.AddImage(mb);




               }
               else
               {
                   MessageBox.Show("导入文件失败，请查找文件并确定其并未被占用");
               }
                

              


            }
          
        }

        static void cbAddImage_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = true;
            e.Handled = true;
            
        }



    }
}
